import json
import os

from options.configs import FrameDataPath
from lib.common.achievePath import Find
from lib.common.smtplib_pack import StpEmail
from settings import emailData


# 发送邮件
def sendEmail():
    temp_file = [0]
    # 在测试报告根路径下寻找到最新的html格式测试报告
    for file in Find.suffixeAbsPaths(FrameDataPath.pathReportRootDir, "html"):
        if (ctime := os.path.getctime(file)) > temp_file[0]:
            temp_file = [ctime, file]
    # 获取测试执行结果统计信息
    with open(os.path.join(FrameDataPath.pathReportRootDir, "history.json"), "r", encoding="utf-8") as f1:
        info = json.load(f1)[-1]
        # 邮件正文内容
        content = f"""
本次测试报告统计情况
通过率:{info.get("pass_rate")}%
    
总计执行{info.get("all")}个用例
    通过:{info.get("success")}个用例
    失败:{info.get("fail")}个用例
    
    未执行:{info.get("skip")}个用例,{info.get("error")}个用例执行错误


执行时间:{info.get("begin_time")},共计耗时:{info.get("runtime")}
"""
    # 发送邮件
    em = StpEmail(emailData.get("user"), emailData.get("password"), emailData.get("senderEmail"),
                  emailData.get("serverType"))
    em.send(emailData.get("achieveEmail"), [emailData.get("textTitle"), content], temp_file[1])


if __name__ == '__main__':
    sendEmail()
